<?php
include_once 'classes/User.php';

$user = new User();
$action = $_POST['logintype'];
if ($_POST['user'] != '' && $_POST['password'] != '') {
    if ($action == 'uid') {
        $r = $user->loginByUid($_POST['user'], $_POST['password']);
    } else {
        $r = $user->loginByUsername($_POST['user'], $_POST['password']);
    }
} else {
    $r = 4;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />  
        <title>Railway Official Website</title>
        <link rel="stylesheet" href="css/foundation.css"/>
        <link rel="stylesheet" href="css/normalize.css"/>
        <link rel="stylesheet" href="css/jquery-ui.css" />
        <script src="js/vendor/jquery.js"></script>
        <script src="js/ui/jquery-ui.js"></script>
        <script src="js/vendor/custom.modernizr.js"></script>
    </head>
    <body>
        <div>
            <nav class="top-bar">
                <ul class="title-area">
                    <li class="name" onClick="void(0);">
                        <h1><a href="#">Railway Official Website</a></h1>
                    </li>
                    <li class="divider"></li>
                    <li class="toggle-topbar menu-icon">
                        <a href="#"><span>menu</span></a>
                    </li>
                </ul>
                <section class="top-bar-section">
                    <ul class="right">
                        <li class="divider"></li>
                        <li><a href="index.php">Home</a></li>
                        <li class="divider"></li>
                        <li><a href="#">Forum</a></li>
                        <li class="divider"></li>
                        <?php
                        if (isset($_SESSION['username'])) {
                            echo '<li><a href="profile.php?mode=21">My profile</a></li>';
                            echo '<li class="divider"></li>';
                            echo '<li><a href="logout.php">Logout</a></li>';
                            echo '<li class="divider"></li>';
                        }
                        else
                            echo '<li><a href="loginPage.php">Login</a></li>';
                        echo '<li class="divider"></li>';
                        ?>
                    </ul>
                </section>
            </nav>
        </div>
        <div class="row">
            <div class="large-8 panel columns large-offset-2">
                <?php
                if ($r == 4) {
                    echo '<p>Username or password are left empty.</p>';
                    echo '<p><a href="loginPage.php">Click here to go back to previous page</a></p>';
                } elseif ($r == 3) {
                    echo '<script>';
                    echo 'var counter = 5;';
                    echo 'var timer = setInterval(function() {';
                    echo 'console.log(counter);';
                    echo 'counter--;';
                    echo 'if (counter == -1) {';
                    echo 'clearInterval(timer);';
                    echo 'window.location.replace("' . $_SESSION['previouspage'] . '");';
                    echo '} else {';
                    echo 'document.getElementById("count").innerHTML = counter;';
                    echo '}';
                    echo '}, 1000);';
                    echo '</script>';
                    echo '<p>Welcome back, ' . $_SESSION['username'] . '! Redirecting you to previous page in <span id="count">5</span> seconds...</p>';
                    echo '<p><a href="' . $_SESSION['previouspage'] . '">Click here if the page did not redirect you automatically</a></p>';
                } elseif ($r == 2) {
                    echo '<p>User not found. Did you enter the correct uid or username?</p>';
                    echo '<p><a href="loginPage.php">Click here to go back to previous page</a></p>';
                } elseif ($r == 1) {
                    echo '<p>Invalid password. Did you enter the correct password? Check if CAPLOCK is turned on.</p>';
                    echo '<p><a href="loginPage.php">Click here to go back to previous page</a></p>';
                } else {
                    echo '<p>Something went wrong, please contact adminstrator for help.</p>';
                    echo '<p><a href="loginPage.php">Click here to go back to previous page</a></p>';
                }
                if (isset($_SESSION['previouspage'])) {
                    unset($_SESSION['previouspage']);
                }
                ?>
            </div>
        </div>
        <footer class="row">
            <div class="large-12 columns">
                <hr />
                <div class="row">
                    <div class="large-4 columns">
                        <p>&copy; Copyright 2013.</p>
                    </div>
                    <div class="large-6 columns">
                        <ul class="inline-list right">
                            <li><a href="aboutus.php">About Us</a></li>
                            <li><a href="contactus.php">Contact Us</a></li>
                            <li><a href="terms.php">Terms and Condition</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </footer>
        <script src="js/foundation.min.js"></script>
        <script>
                        $(document).foundation();
        </script>
    </body>
</html>

